<?php
class CourtsController extends AppController {

	var $name = 'Courts';

	function index() {
             $this->set('title_for_layout', 'Datos Judiciales - Juzgados');
		$this->Court->recursive = 0;
		$this->set('courts', $this->paginate());
	}

	function view($id = null) {
              $this->set('title_for_layout', 'Datos Judiciales - Juzgados');
		if (!$id) {
			$this->Session->setFlash(__('Invalid court', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('court', $this->Court->read(null, $id));
	}

	function add() {
              $this->set('title_for_layout', 'Datos Judiciales - Juzgados');
		if (!empty($this->data)) {
			$this->Court->create();
			if ($this->Court->save($this->data)) {
				$this->Session->setFlash(__('The court has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The court could not be saved. Please, try again.', true));
			}
		}
		$charters = $this->Court->Placements->find('list',array('fields'=>'descripcion'));
		$this->set(compact('charters'));
	}

	function edit($id = null) {
              $this->set('title_for_layout', 'Datos Judiciales - Juzgados');
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid court', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Court->save($this->data)) {
				$this->Session->setFlash(__('The court has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The court could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Court->read(null, $id);
		}
		$charters = $this->Court->Placements->find('list',array('fields'=>'descripcion'));
		$this->set(compact('charters'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for court', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Court->delete($id)) {
			$this->Session->setFlash(__('Court deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Court was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
